package com.woniuxy.controller;


import com.woniuxy.demo.Result;
import com.woniuxy.dto.UserRelatedDto.UserTableDto;
import com.woniuxy.dto.commonDto.BaseDto;
import com.woniuxy.dto.DistributorDtos;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.DistributorException;
import com.woniuxy.param.DelDistributorParam;
import com.woniuxy.param.DistributorAddPagram;
import com.woniuxy.param.DistributorPageParam;
import com.woniuxy.param.DistributorupdateParam;
import com.woniuxy.service.DistributorService;
import com.woniuxy.service.Impl.DistributorServiceImpl;
import com.woniuxy.util.ValidatedUtil;
import com.woniuxy.util.WoniuBeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 渠道商 前端控制器
 * </p>
 *
 * @author zcx, ftl, gr, rhs
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/distributor")
@Api(tags = "渠道商相关接口")
public class DistributorController {


    @Resource
    private DistributorService distributorService;

    /**
     * 根据id查询渠道商
     * @param distributorId
     * @return
     */
    @ApiOperation("根据id查询渠道商")
    @PostMapping("/findDistributorById/{distributorId}")
    @ApiImplicitParam(name = "distributorId", value = "渠道商id")
    public Result findDistributorById(@PathVariable Integer distributorId)  {

        DistributorDtos dtos = distributorService.findDistributorById(distributorId);

        return Result.newInstance().setMsg("查询成功").setCode(200).setData(dtos);
    }

    /**
     * 分页获取渠道商信息
     *
     * @param dp
     * @return
     */
    @ApiOperation("分页获取渠道商信息")
    @PostMapping("/getdistributor")
    public Result getdistributors(@RequestBody DistributorPageParam dp) {
        BaseDto<DistributorDtos> distributors = distributorService.getDistributors(dp);
        return Result.newInstance().setCode(200).setMsg("查询成功").setData(distributors);
    }

    /**
     * 添加渠道商信息
     *
     * @param dap
     * @return
     */
    @ApiOperation("添加渠道商信息")
    @PostMapping("/adddistributor")
    public Result adddistributor(@RequestBody @Validated DistributorAddPagram dap, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);
        if (distributorService.adddistributor(dap)) {
            DistributorDtos distributorDtos = WoniuBeanUtils.copyObject(dap, DistributorDtos.class);
            return new Result().setCode(200).setMsg("添加成功").setData(distributorDtos);
        }
        throw new DistributorException(CodeEnum.DISTRIBUTOR_SAVE_FAIL_5.getMessage()
                , CodeEnum.DISTRIBUTOR_SAVE_FAIL_5.getCode());
    }

    /**
     * 删除渠道商
     *
     * @param ids
     * @return
     */
    @ApiOperation("删除渠道商")
    @PostMapping("/deldistributors")
    public Result deldistributor(@RequestBody List<Integer> ids) {
        if (distributorService.deldistributor(ids)) {
            return new Result().setCode(200).setMsg("删除成功");
        }
        throw new DistributorException(CodeEnum.DISTRIBUTOR_DELETE_FAIL_3.getMessage()
                , CodeEnum.DISTRIBUTOR_DELETE_FAIL_3.getCode());
    }

    /**
     * 修改渠道商
     *
     * @param dp
     * @return
     */
    @ApiOperation("修改渠道商")
    @PostMapping("/updatedistributors")
    public Result updatedistributor(@RequestBody @Validated DistributorupdateParam dp, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);
        if (distributorService.updatedistributor(dp)) {
            return new Result().setCode(200).setMsg("修改成功");
        }
        throw new DistributorException(CodeEnum.DISTRIBUTOR_UPDATE_FAIL.getMessage(), CodeEnum.DISTRIBUTOR_UPDATE_FAIL.getCode());

    }

}

